package com.xuhuan.demo.digest;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.logging.log4j.util.Base64Util;
import org.junit.Test;

/**
 * @author: 徐欢
 * @date: 2021/7/27 20:40
 * @description: 加密工具类
 */
@Slf4j
public class DigestUtilsDemo {
    /**
     * MD5加密，返回32位字符串
     */
    @Test
    public void testMd5() {
        log.info("password加密后：[{}]", DigestUtils.md5("password"));
    }

    /**
     * MD5加密，返回32位字符串
     */
    @Test
    public void testMd5Hex() {
        log.info("password加密后：[{}]", DigestUtils.md5Hex("password"));
    }

    /**
     * SHA-1加密
     */
    @Test
    public void testSha1Hex() {
        log.info("password加密后：[{}]", DigestUtils.sha1("password"));
    }

    /**
     * SHA-256加密
     */
    @Test
    public void testSha256Hex() {
        log.info("password加密后：[{}]", DigestUtils.sha256("password"));
    }

    /**
     * SHA-512加密
     */
    @Test
    public void testSha512Hex() {
        log.info("password加密后：[{}]", DigestUtils.sha512("password"));
    }

    @Test
    public void testBase64(){
        String password="1234qwer";
        String encodePassword = Base64.getEncoder().encodeToString(password.getBytes(StandardCharsets.UTF_8));
        System.out.println(encodePassword);

        String pw = new String(Base64.getDecoder().decode(encodePassword), StandardCharsets.UTF_8);
        System.out.println(pw);
    }
}
